package com.xiaochong.tir.apiservice.controller;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.apiservice.scheduled.CmcScheduled;
import com.xiaochong.tir.apiservice.service.CmcDataDealService;
import com.xiaochong.tir.apiservice.service.CurrencyInfoService;
import com.xiaochong.tir.apiservice.service.KLineService;
import com.xiaochong.tir.apiservice.service.MakeTradesDataService;
import com.xiaochong.tir.apiservice.util.HttpUtils;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.util.KLineTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.Semaphore;


/**
 * Created by wujiaxing on 2018/5/24.
 * akka配置接口
 */
@RestController
@RequestMapping("/config")
public class AkkaConfigController {

    @Autowired
    private KLineService kLineService;

    @Autowired
    CmcDataDealService cmcDataDealService;

    @Autowired
    private CurrencyInfoService currencyInfoService;

    @Autowired
    CmcScheduled cmcScheduled;
    @Autowired
    private MakeTradesDataService makeTradesDataService;

    public  static  final Integer COUNT = 1;
    public  static Semaphore semaphoreInitK = new Semaphore(COUNT);

    public  static Semaphore semaphoreInitCmcKline = new Semaphore(COUNT);

    @GetMapping("/akkaSwitch")
    public boolean goodakkaSwitchsPage() {
        HttpUtils.setAkkaFlag(!HttpUtils.akkaFlag);
        return HttpUtils.akkaFlag;
    }

    @GetMapping("/initKlineData")
    public  String initKlineData(@RequestParam(value = "time") String time , @RequestParam(value = "endTime") String endTime ){
//        String str = "1986-04-08 12";
        try {
            if(semaphoreInitK.tryAcquire(COUNT)){
//                semaphoreInitK.acquire(COUNT);
                new Thread("Thread-initKlineData"){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            LogTrace.beginTrace();
                            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
                            //开始初始化的时间
                            LocalDateTime dateDate = LocalDateTime.parse(time, formatter);


                            LocalDateTime nowDate =  LocalDateTime.parse(endTime, formatter);

                            while (nowDate.compareTo(dateDate)>= 0){
                                switch (dateDate.getHour()){
                                    case 0:
                                        kLineService.dayLineScheduled(8 , KLineTypeEnum.HOUR83 , dateDate);
                                        kLineService.dayLineScheduled(12 , KLineTypeEnum.DAY_HALF2 , dateDate);
                                        kLineService.dayLineScheduled(24 , KLineTypeEnum.DAY , dateDate);
                                        break;
                                    case 8 :
                                        kLineService.dayLineScheduled(8 , KLineTypeEnum.HOUR81 , dateDate);
                                        break;
                                    case 12 :
                                        kLineService.dayLineScheduled(12 , KLineTypeEnum.DAY_HALF1 , dateDate);
                                        break;
                                    case 16:
                                        kLineService.dayLineScheduled(8 , KLineTypeEnum.HOUR82 , dateDate);
                                        break;

                                }
                                dateDate = dateDate.plusHours(1);
                            }

                            LogTrace.endTrace();
                        }catch (Exception e){
                            LogTrace.error(e.getMessage() ,e);
                        }finally {
                            semaphoreInitK.release();
                        }
                    }
                }.start();
                return "start:initKlineData";
            }else {
                return "running";
            }
        }catch (Exception e){
            return e.getLocalizedMessage();
        }
    }


    @GetMapping("/initCmcKlineData")
    public String initCmcKlineData(@RequestParam(value = "time") String time , @RequestParam(value = "endTime") String endTime ){

        try {

            if(semaphoreInitCmcKline.tryAcquire(COUNT)){
//                semaphoreInitCmcKline.acquire(COUNT);
                new Thread("Thread-initCmcKlineData"){
                    @Override
                    public void run() {
                        super.run();
                        LogTrace.beginTrace();
                        try {
                            CurrencyInfo currencyInfo = new CurrencyInfo();
                            currencyInfo.setEnableFlag(true);
                            List<CurrencyInfo> currencyInfoList = currencyInfoService.selectByEntityList(currencyInfo);

                            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
                            //开始初始化的时间
                            LocalDateTime dateDate = LocalDateTime.parse(time, formatter);


                            LocalDateTime nowDate =  LocalDateTime.parse(endTime, formatter);

                            while (nowDate.compareTo(dateDate)>= 0) {
                                if (currencyInfoList != null && currencyInfoList.size() > 0) {

                                    for (CurrencyInfo currencyInfoBase : currencyInfoList) {
                                        cmcDataDealService.cmcKDataDay(dateDate ,  currencyInfoBase.getCmcId());
                                    }
                                }
                                dateDate = dateDate.plusDays(1);
                            }
                        } finally {
                            semaphoreInitCmcKline.release(COUNT);
                        }
                    }
                }.start();
                return  "start:initCmcKlineData";
            }else {
                return  "running";
            }
        }catch (Exception e){
            return e.getLocalizedMessage();
        }
    }


    @GetMapping("/cmcMaData")
    public String cmcMaData(){
        LogTrace.beginTrace();
        cmcScheduled.cmcMaData();
        return  "success:cmcMaData";
    }
    @GetMapping("/makeTradesData")
    public String makeTradesData(@RequestParam(value = "exchangeId",required =  false)Integer exchangeId){
        LogTrace.beginTrace();
        String ss = makeTradesDataService.makeTradesData(exchangeId);
        LogTrace.info("初始化历史交易5分钟数据","end","success");
        return  "success";
    }
    @GetMapping("/makeTradesChangeData")
    public String makeTradesChangeData(@RequestParam(value = "exchangeId",required =  false)Integer exchangeId){
        LogTrace.beginTrace();
        String ss = makeTradesDataService.makeTradesChangeData(exchangeId);
        LogTrace.info("初始化历史交易-24Rowkey","end","success");
        return  "success";
    }
}
